package boundary;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import control.ChartController;

import entity.GameMovableObj;
import entity.Pecies;

public class ChartPanel extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private int foo;
	private static final long serialVersionUID = 100L;
	private static int panelWidth = 800, panelHight=600;
	private ArrayList<GameMovableObj> allPecies = GameMovableObj.getGameMovableObj();
	private ChartController chartControl = new ChartController();
	JLabel playerLaLabel;
	private JFrame frame = new JFrame("Result Analysis");
	private ShowChartPanel showChart = new ShowChartPanel();
	private JPanel inputBar = new JPanel();
	private JPanel mainPanel = new JPanel();
	String[] playerType = new String[allPecies.size()];
	JComboBox playerList;
	
	Component chartComponent = null;
	
	public ChartPanel(){
		frame.setContentPane(mainPanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(this);
		frame.setSize(panelWidth, panelHight);
		frame.setVisible(true);
		
                Pecies showPlayer = (Pecies)allPecies.get(0);
		showChart.setCurrentPlayer((Pecies)allPecies.get(0));
                if(showPlayer.getEventLog().size()>=2){
                  chartControl.showPopup(showPlayer);
                 }
                
		for(int i=0;i<allPecies.size();i++){
			Pecies p = (Pecies)allPecies.get(i);
			playerType[i] = p.getName();
		}
		playerList = new JComboBox(playerType);
		inputBar.setLayout(new FlowLayout());
		inputBar.add(playerLaLabel);
		inputBar.add(playerList);
		
		playerList.addActionListener(this);	
		
		setLayout(new BorderLayout());
		frame.setLayout(new GridBagLayout()); 
		GridBagConstraints c = new GridBagConstraints(); 
		/**make this component tall*/
		c.fill = GridBagConstraints.HORIZONTAL;     c.ipady = 250;       
		c.weightx = 0.0;     c.gridwidth = 800;     c.gridx = 0;     c.gridy = 0;
		frame.add(inputBar,c);
		/**make this component tall*/ 
		c.fill = GridBagConstraints.HORIZONTAL;     c.ipady = 450;      
		c.weightx = 600.0;     c.gridwidth = 800;     c.gridx = 0;     c.gridy = 1;
		frame.add((JPanel)showChart,c); 
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	}
	
	public void paint(Graphics g) {
		  super.paint(g);       
	 }
	  
	  @Override
	  public void actionPerformed(ActionEvent e) {
              Pecies showPlayer = (Pecies)allPecies.get(playerList.getSelectedIndex());
              showChart.setCurrentPlayer(showPlayer);
              if(showPlayer.getEventLog().size()>=2){
                  chartControl.showPopup(showPlayer);
                 }
              super.validate();
              showChart.repaint();
	  }
	
}
